<?php

namespace console\controllers;

use admin\models\Adminer;
use common\models\db\Banners;
use common\components\wtools\tools\ArrayHelper;
use common\components\wtools\tools\Security;
use common\datas\Enums;
use common\jobs\DemoJob;
use common\models\db\Adminers;
use common\models\db\Categories;
use common\models\db\Products;
use common\models\db\User;
use common\Tools;
use yii\console\Controller;
use yii\helpers\Console;

class TestController extends Controller
{
    public function actionTest()
    {
        dd(uniqid('u', true));
    }

    /**
     * 创建后台管理员
     * php yii test/add-adminer
     * @return void
     * @throws \yii\base\Exception
     */
    public function actionAddAdminer()
    {
        $adminer = new Adminers();
        $mobile = Console::input("请输入11位手机号：");
        $account_name = Console::input("请输入账户名：");
        $password = Console::input("请输入密码：");
        $adminer->mobile = $mobile;
        $adminer->account_name = $account_name;
        $adminer->generatePassword($password);
        $adminer->status = $adminer::STATUS_ACTIVE;
        $adminer->auth_key = Tools::generateHasDateUniqueString();
        if (!$adminer->save()) {
            dd($adminer->errors);
        }
    }

    /**
     * 修改后台管理员密码
     * php yii test/update-adminer
     * @return void
     * @throws \yii\base\Exception
     */
    public function actionUpdateAdminer()
    {
        $mobile = Console::input("请输入11位手机号：");
        $password = Console::input("请输入密码：");
        $adminer = Adminer::findByMobile($mobile);
        $adminer->generatePassword($password);
        $adminer->status = $adminer::STATUS_ACTIVE;
        $adminer->auth_key = Tools::generateHasDateUniqueString();
        if (!$adminer->save()) {
            dd($adminer->errors);
        }
    }

    /**
     * 添加分类
     * php yii test/add-category
     * @return void
     */
    public function actionAddCategory()
    {
        exit;
//        $cateName = Console::input("分类名:");
        $cateNames = ['客厅', '卧室', '茶几', '边几', '桌子', '柜子', '五金', '案例'];
        foreach ($cateNames as $cateName) {
            $category = new Categories();
            $category->name = $cateName;
            $category->is_root = Enums::IS_TRUE;
            $category->status = Categories::STATUS_ACTIVE;
            if (!$category->save()) {
                dd($category->errors);
            }
        }
    }

    /**
     * 解密密码
     * php yii test/decrypt-pwd
     * @return void
     */
    public function actionDecryptPwd()
    {
        $pwd = Console::input("密码：");
        $x = Security::thinkDecrypt($pwd);
        var_dump($x);
    }

    /**
     * 添加产品
     * php yii test/add-products
     * @return void
     */
    public function actionAddProducts()
    {
        $count = Console::input("个数:");
        $categortArr = ArrayHelper::getColumn(Categories::find()->all(), 'id');
        $user = Adminers::find()->one();
        for ($i = 0; $i < $count; $i ++) {
            $product = new Products();
            $product->name = '测试产品'. date("YmdHis-") . $i;
            $product->cover = \Yii::$app->urlManagerXcxApi->createAbsoluteUrl(['/guest/pub/gd', 'w' => 300]);
            $product->cate_root = array_rand($categortArr);
            $product->status = Products::STATUS_ACTIVE;
            $product->product_sort = 1;
            $product->created_by_admin = $user->id;
            $product->updated_by_admin = $user->id;
            $product->detachBehavior('blameable');
            if (!$product->save()) {
                dd($product->errors);
            }
            var_dump($product->id);
        }
    }

    /**
     * 生成后台管理员token
     * php yii test/adminers-token
     * @return void
     * @throws \yii\base\Exception
     */
    public function actionAdminersToken()
    {
        $adminers = Adminers::find()->all();
        foreach ($adminers as $k => $adminer) {
            $adminer->generateToken();
            $adminer->save();
        }
    }

    public function actionTest1()
    {
        $key = "Test-test1";
        \Yii::$app->mutex->acquire($key, 30);
        var_dump(123456789);
        sleep(5);
    }

    public function actionTest2()
    {
        $job = new DemoJob();
        $job->id = 1;
        \Yii::$app->queue->push($job);
    }

    public function actionTest3()
    {
        dd(\Yii::t('app', 'Token'));
    }

    public function actionTest4()
    {
        dd(Tools::generateHasDateUniqueString());
    }

    public function actionTest5()
    {
        dd(\Yii::$app->xcxApi->guestPost('public/test', ['x' => 123456]));
    }

    public function actionTest6()
    {
        $products = Products::find()->all();
        foreach ($products as $k => $product) {
            $product->cover = str_replace('dev.lzdls.com', 'www.lzdls.com', $product->cover);
            $product->save();
        }
        $banners = Banners::find()->all();
        foreach ($banners as $k => $banner) {
            $banner->url = str_replace('dev.lzdls.com', 'www.lzdls.com', $banner->url);
            $banner->save();
        }
    }

    public function actionTest7()
    {
        $file = \Yii::getAlias('@uploads_root/files/20240312/b5ce8161f65cc555a6223f37d303ef38.5614.jpg');
        $file = str_replace('\\', '/', $file);
        $url = \Yii::$app->fs->uploadByFileRoot($file);
        dd($url);
    }
}
